import 'dart:ui';

import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/services.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
import 'package:www.znpigai.teacher/utils/http.dart';
import 'package:www.znpigai.teacher/utils/nav.dart';

abstract class GlobalBaseState {
  Color get themeColor;

  set themeColor(Color color);

  // userInfo这种复杂类型不支持更新，目前只用于全局存储，页面要手动刷新
  Map<String, dynamic> get userInfo;

  set userInfo(Map<String, dynamic> info);
}

class GlobalState implements GlobalBaseState, Cloneable<GlobalState> {
  static const firstPageIndex = 1;
  static const pageSize = 10;
  static const DEBUG_BASE_URL = "http://test.znpigai.com";
  static const PRODUCT_BASE_URL = "https://www.znpigai.com";
  static const articleTypes = {
    'type1': "记叙文",
    'type3': "说明文",
    'type4': "议论文",
    'type5': "应用文",
    'type7': "散文"
  };
  static const isTeacher = false;

  static String get userRole => isTeacher ? 'teacher' : 'student';

  static Map<String, dynamic> systemConst = {};
  static String wxCode;
  static String wxErr;
  static bool wxInstalled = false;
  static SharedPreferences _prefs;

  static Map<String, String> tags = {};

  static Map<String, String> themes = {};

  static Map<String, String> types = {
    "type1": "记叙文",
    "type1_1": "记叙文写人",
    "type1_2": "记叙文写事",
    "type1_3": "记叙文写景",
    "type1_4": "记叙文状物",
    "type1_5": "记叙文童话故事",
    "type1_6": "记叙文想象作文",
//      "type2": "",
    "type3": "说明文",
    "type4": "议论文",
    "type4_1": "议论文想象类",
    "type4_2": "议论文寓言故事类",
    "type4_3": "议论文观点言论类",
    "type5": "应用文",
    "type5_1": "应用文书信",
    "type5_2": "应用文便条",
    "type5_3": "应用文通知",
    "type5_4": "应用文生活日记",
    "type5_5": "应用文观后感",
    "type5_6": "应用文读后感",
    "type5_7": "应用文表扬信",
    "type5_8": "应用文建议书",
    "type5_9": "应用文解说词",
    "type5_10": "应用文观察日记",
    "type5_11": "应用文新闻稿",
    "type5_12": "应用文发言稿",
    "type5_13": "应用文邀请函",
    "type5_14": "应用文演讲稿",
    "type5_15": "应用文请假条",
    "type5_16": "应用文招领启事",
    "type5_17": "应用文推荐信",
//      "type6": "",
    "type7": "散文"
  };

  static Map<String, String> grades = {
    "grade1": "一年级",
    "grade2": "二年级",
    "grade3": "三年级",
    "grade4": "四年级",
    "grade5": "五年级",
    "grade6": "六年级",
    "grade7": "初一",
    "grade8": "初二",
    "grade9": "初三",
    "grade10": "高一",
    "grade11": "高二",
    "grade12": "高三",
    "grade13": "大学以上",
  };

  /// app初始化，获取必要信息
  static init() async {
    _prefs = await SharedPreferences.getInstance();

    /// 强制竖屏
    await SystemChrome.setPreferredOrientations(
        [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

    /// 检查微信
    GlobalState.wxInstalled = await fluwx.isWeChatInstalled();

    /// 初始化ticket
    var ticket = GlobalState.ticket;
    if (ticket == null || ticket.isEmpty) {
      var uuid = new Uuid();
      GlobalState.ticket = uuid.v4(); // random
    }

    /// 读取全局sys变量
    Http.get("/correct/pub/api/sys.mvc").then((result) {
      systemConst = result.data;

      systemConst['WritingReferenceTag'].forEach((tag) {
        tags[tag['name']] = tag['text'];
      });

      systemConst['WritingReferenceTheme'].forEach((tag) {
        themes[tag['name']] = tag['text'];
      });
    });
  }

  static String baseUrl(String url) {
    var host = isInDebugMode ? PRODUCT_BASE_URL : PRODUCT_BASE_URL;
    var ret = '';

    if (url != null && url.startsWith("http")) {
      ret = url;
    } else {
      ret = host + url;
    }

    return ret;
  }

  static bool get isInDebugMode {
    bool inDebugMode = false;
    assert(inDebugMode = true);
    return inDebugMode;
  }

  // isSignIn
  static bool get isSignIn => _prefs.getBool("isSignIn") ?? false;

  static set isSignIn(bool signin) => _prefs.setBool("isSignIn", signin);

  // ticket
  static String get ticket => _prefs.getString("ticket");

  static set ticket(String t) => _prefs.setString("ticket", t);

  // cookie
  static String get cookie => _prefs.getString("cookie");

  static set cookie(String c) => _prefs.setString("cookie", c);

  // firstBoot
  static bool get isFirstBoot => _prefs.getBool("isFirstBoot") ?? true;

  static set isFirstBoot(bool f) => _prefs.setBool("isFirstBoot", f);

  // ticket
  static String get mobile => _prefs.getString("mobile");

  static set mobile(String t) => _prefs.setString("mobile", t);

  // dark
  static double get dark => _prefs.getDouble("dark") ?? 0;

  static set dark(double f) => _prefs.setDouble("dark", f);

  @override
  Color themeColor;

  @override
  Map<String, dynamic> userInfo;

  @override
  GlobalState clone() {
    return GlobalState()
      ..themeColor = themeColor
      ..userInfo = userInfo;
  }

  /// 根据登录情况设置启动页面
  static String getHomePage() {
    var home = '';
    if (GlobalState.isFirstBoot) {
      home = Nav.BOOT_GUIDE;
    } else if (GlobalState.isSignIn) {
      home = Nav.HOME;
    } else if (GlobalState.isTeacher) {
      home = Nav.SOCIAL_LOGIN;
    } else {
      home = Nav.LOGIN;
    }
    return home;
  }
}
